package com.minsu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.minsu.entity.Listing;
import com.minsu.mapper.ListingMapper;
import com.minsu.service.ListingService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import com.minsu.dto.ListingImportDTO;
import com.minsu.listener.ListingImportListener;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ListingServiceImpl extends ServiceImpl<ListingMapper, Listing> implements ListingService {

    @Override
    public Page<Listing> search(Page<Listing> page, String title, BigDecimal minPrice, BigDecimal maxPrice, String district) {
        LambdaQueryWrapper<Listing> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Listing::getTitle, title);
        }
        
        if (minPrice != null) {
            queryWrapper.ge(Listing::getPrice, minPrice);
        }
        
        if (maxPrice != null) {
            queryWrapper.le(Listing::getPrice, maxPrice);
        }
        
        if (StringUtils.hasText(district)) {
            queryWrapper.eq(Listing::getLocation, district);
        }
        
        return this.page(page, queryWrapper);
    }

    @Override
    public List<String> listDistricts() {
        return baseMapper.selectDistinctDistricts();
    }

    @Override
    public Map<String, Object> importExcel(MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        
        if (file == null || file.isEmpty()) {
            result.put("success", false);
            result.put("message", "请选择要上传的文件");
            return result;
        }

        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || (!originalFilename.endsWith(".xlsx") && !originalFilename.endsWith(".xls"))) {
            result.put("success", false);
            result.put("message", "请上传Excel文件");
            return result;
        }

        try {
            ListingImportListener listener = new ListingImportListener(this);
            EasyExcel.read(file.getInputStream(), ListingImportDTO.class, listener).sheet().doRead();
            
            List<String> errorMessages = listener.getErrorMessages();
            if (!errorMessages.isEmpty()) {
                result.put("success", false);
                result.put("message", "部分数据导入失败");
                result.put("errors", errorMessages);
            } else {
                result.put("success", true);
                result.put("message", "数据导入成功");
            }
        } catch (IOException e) {
            log.error("Excel导入失败", e);
            result.put("success", false);
            result.put("message", "文件读取失败：" + e.getMessage());
        } catch (Exception e) {
            log.error("Excel导入失败", e);
            result.put("success", false);
            result.put("message", "导入失败：" + e.getMessage());
        }
        
        return result;
    }
} 